package com.wpen.unit.infra.repository.job.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.JobExec1LogMapper;
import com.wpen.unit.infra.mybatis.job.po.JobExec1LogPo;
import com.wpen.unit.infra.mybatis.job.po.JobExec1LogPo.JobExec1LogColumns;
import com.wpen.unit.infra.repository.job.JobExec1LogRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 任务作业执行1日志维护.
 * 
 * @author Wang Peng
 * @date 2022-12-06
 *
 */
@Repository
@Transactional
public class JobExec1LogRepositoryImpl extends ServiceImpl<JobExec1LogMapper, JobExec1LogPo>
		implements JobExec1LogRepository {

	@Override
	public JobExec1LogPo queryByIdAndTime(Serializable id, Long taskTriggerTime) {
		QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(JobExec1LogColumns.ID.name(), id);
		queryWrapper.eq(JobExec1LogColumns.TASK_TRIGGER_TIME.name(), taskTriggerTime);
		return getBaseMapper().selectOne(queryWrapper);
	}

	@Override
	public boolean updateByIdWithNull(JobExec1LogPo jobExec1LogPo) {
		// 根据ID更新所有字段，空值也更新
		int num = getBaseMapper().alwaysUpdateSomeColumnById(jobExec1LogPo);
		boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public List<JobExec1LogPo> queryListByBatchId(Long batchLogId, Long batchTriggerTime) {
		QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(JobExec1LogColumns.BATCH_EXEC_LOG_ID.name(), batchLogId);
		queryWrapper.eq(JobExec1LogColumns.BATCH_TRIGGER_TIME.name(), batchTriggerTime);
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public List<JobExec1LogPo> queryListByBatchId(Long batchLogId, List<Long> taskLogIds) {
		QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
		queryWrapper.eq(JobExec1LogColumns.BATCH_EXEC_LOG_ID.name(), batchLogId);
		queryWrapper.in(JobExec1LogColumns.TASK_EXEC_LOG_ID.name(), taskLogIds);
		return getBaseMapper().selectList(queryWrapper);
	}

	@Override
	public long count(JobExec1LogPo jobExec1LogPo) {
		QueryWrapper<JobExec1LogPo> queryWrapper = createQuery(jobExec1LogPo);
		return super.count(queryWrapper);
	}

	@Override
	public List<JobExec1LogPo> queryList(int index, int size, JobExec1LogPo jobExec1LogPo) {
		QueryWrapper<JobExec1LogPo> queryWrapper = createQuery(jobExec1LogPo);
		if (size > 0) {
			Page<JobExec1LogPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		// queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
	}

	private QueryWrapper<JobExec1LogPo> createQuery(JobExec1LogPo jobExec1LogPo) {
		QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
		if (jobExec1LogPo.getId() != null) {
			queryWrapper.eq(JobExec1LogColumns.ID.name(), jobExec1LogPo.getId());
		}
		if (jobExec1LogPo.getTaskExecLogId() != null) {
			queryWrapper.eq(JobExec1LogColumns.TASK_EXEC_LOG_ID.name(), jobExec1LogPo.getTaskExecLogId());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getBizCode())) {
			queryWrapper.eq(JobExec1LogColumns.BIZ_CODE.name(), jobExec1LogPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getBatchCode())) {
			queryWrapper.eq(JobExec1LogColumns.BATCH_CODE.name(), jobExec1LogPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getTaskCode())) {
			queryWrapper.eq(JobExec1LogColumns.TASK_CODE.name(), jobExec1LogPo.getTaskCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getJobCode())) {
			queryWrapper.eq(JobExec1LogColumns.JOB_CODE.name(), jobExec1LogPo.getJobCode());
		}
		if (jobExec1LogPo.getExecDate() != null) {
			queryWrapper.eq(JobExec1LogColumns.EXEC_DATE.name(), jobExec1LogPo.getExecDate());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getExecStatus())) {
			queryWrapper.eq(JobExec1LogColumns.EXEC_STATUS.name(), jobExec1LogPo.getExecStatus());
		}
		// if (StringUtil.isNotEmpty(jobExec1LogPo.getExecResultDesc())) {
		// queryWrapper.eq(JobExec1LogColumns.EXEC_RESULT_DESC.name(),
		// jobExec1LogPo.getExecResultDesc());
		// }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getStartupTime())) {
//            queryWrapper.eq(JobExec1LogColumns.STARTUP_TIME.name(), jobExec1LogPo.getStartupTime());
//        }
		// if (StringUtil.isNotEmpty(jobExec1LogPo.getEndTime())) {
		// queryWrapper.eq(JobExec1LogColumns.END_TIME.name(),
		// jobExec1LogPo.getEndTime());
		// }
		return queryWrapper;
	}

	@Override
	public JobExec1LogPo queryFirst(JobExec1LogPo jobExec1LogPo) {
		QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
		if (jobExec1LogPo.getId() != null) {
			queryWrapper.eq(JobExec1LogColumns.ID.name(), jobExec1LogPo.getId());
		}
		if (jobExec1LogPo.getTaskExecLogId() != null) {
			queryWrapper.eq(JobExec1LogColumns.TASK_EXEC_LOG_ID.name(), jobExec1LogPo.getTaskExecLogId());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getBizCode())) {
			queryWrapper.eq(JobExec1LogColumns.BIZ_CODE.name(), jobExec1LogPo.getBizCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getBatchCode())) {
			queryWrapper.eq(JobExec1LogColumns.BATCH_CODE.name(), jobExec1LogPo.getBatchCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getTaskCode())) {
			queryWrapper.eq(JobExec1LogColumns.TASK_CODE.name(), jobExec1LogPo.getTaskCode());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getJobCode())) {
			queryWrapper.eq(JobExec1LogColumns.JOB_CODE.name(), jobExec1LogPo.getJobCode());
		}
		if (jobExec1LogPo.getExecDate() != null) {
			queryWrapper.eq(JobExec1LogColumns.EXEC_DATE.name(), jobExec1LogPo.getExecDate());
		}
		if (StringUtil.isNotEmpty(jobExec1LogPo.getExecStatus())) {
			queryWrapper.eq(JobExec1LogColumns.EXEC_STATUS.name(), jobExec1LogPo.getExecStatus());
		}
		// if (StringUtil.isNotEmpty(jobExec1LogPo.getExecResultDesc())) {
		// queryWrapper.eq(JobExec1LogColumns.EXEC_RESULT_DESC.name(),
		// jobExec1LogPo.getExecResultDesc());
		// }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getStartupTime())) {
//            queryWrapper.eq(JobExec1LogColumns.STARTUP_TIME.name(), jobExec1LogPo.getStartupTime());
//        }
		// if (StringUtil.isNotEmpty(jobExec1LogPo.getEndTime())) {
		// queryWrapper.eq(JobExec1LogColumns.END_TIME.name(),
		// jobExec1LogPo.getEndTime());
		// }
		// queryWrapper.orderBy(true, false, JobExec1LogPoColumns.ID.name());
		return super.getOne(queryWrapper);
	}

	@Override
	public JobExec1LogPo queryById(Serializable id) {
		return super.getById(id);
	}

	@Override
	public boolean insert(JobExec1LogPo jobExec1LogPo) {
//        jobExec1LogPo.setVersion(1);
//        jobExec1LogPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
		return super.save(jobExec1LogPo);
	}

	@Override
	public boolean insertBatch(List<JobExec1LogPo> listJobExec1LogPos) {
		int rs = getBaseMapper().insertBatchSomeColumn(listJobExec1LogPos);
		return SqlHelper.retBool(rs);
	}

	@Override
	public boolean updateById(JobExec1LogPo jobExec1LogPo) {
		UpdateWrapper<JobExec1LogPo> updateWrapper = Wrappers.update();
		updateWrapper.eq(JobExec1LogColumns.ID.name(), jobExec1LogPo.getId());
//        updateWrapper.eq(JobExec1LogColumns.VERSION.name(), jobExec1LogPo.getVersion());
//        jobExec1LogPo.setVersion(jobExec1LogPo.getVersion() + 1);
		boolean rs = super.update(jobExec1LogPo, updateWrapper);
		// 根据ID更新所有字段，空值也更新
		// int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
		// boolean rs = SqlHelper.retBool(num);
		if (!rs) {
			throw new UpdateZeroException();
		}
		return rs;
	}

	@Override
	public boolean deleteByIds(Serializable[] ids) {
		UpdateWrapper<JobExec1LogPo> updateWrapper = Wrappers.update();
		updateWrapper.in(JobExec1LogColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
	}

//    public JobExec1LogPo queryByExample(JobExec1LogPo jobExec1LogPo) {
//        QueryWrapper<JobExec1LogPo> queryWrapper = Wrappers.query();
//        if (jobExec1LogPo.getId() != null) {
//            queryWrapper.eq(JobExec1LogColumns.ID.name(), jobExec1LogPo.getId());
//        }
//        if (jobExec1LogPo.getTaskExecLogId() != null) {
//            queryWrapper.eq(JobExec1LogColumns.TASK_EXEC_LOG_ID.name(), jobExec1LogPo.getTaskExecLogId());
//        }
//        if (jobExec1LogPo.getAgentExecLogId() != null) {
//            queryWrapper.eq(JobExec1LogColumns.AGENT_EXEC_LOG_ID.name(), jobExec1LogPo.getAgentExecLogId());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getBizCode())) {
//            queryWrapper.eq(JobExec1LogColumns.BIZ_CODE.name(), jobExec1LogPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getBatchCode())) {
//            queryWrapper.eq(JobExec1LogColumns.BATCH_CODE.name(), jobExec1LogPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getTaskCode())) {
//            queryWrapper.eq(JobExec1LogColumns.TASK_CODE.name(), jobExec1LogPo.getTaskCode());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getJobCode())) {
//            queryWrapper.eq(JobExec1LogColumns.JOB_CODE.name(), jobExec1LogPo.getJobCode());
//        }
//        if (jobExec1LogPo.getExecDate() != null) {
//            queryWrapper.eq(JobExec1LogColumns.EXEC_DATE.name(), jobExec1LogPo.getExecDate());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getExecStatus())) {
//            queryWrapper.eq(JobExec1LogColumns.EXEC_STATUS.name(), jobExec1LogPo.getExecStatus());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getExecResultDesc())) {
//            queryWrapper.eq(JobExec1LogColumns.EXEC_RESULT_DESC.name(), jobExec1LogPo.getExecResultDesc());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getStartupTime())) {
//            queryWrapper.eq(JobExec1LogColumns.STARTUP_TIME.name(), jobExec1LogPo.getStartupTime());
//        }
//        if (StringUtil.isNotEmpty(jobExec1LogPo.getEndTime())) {
//            queryWrapper.eq(JobExec1LogColumns.END_TIME.name(), jobExec1LogPo.getEndTime());
//        }
//		Page<JobExec1LogPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, JobExec1LogPoColumns.ID.name());
//    }

}
